package com.Drools.CIPS.ruler;

import com.Drools.CIPS.bean.DroolsReturn;
import com.Drools.CIPS.util.XmlToMap;
import com.prowidesoftware.swift.model.SwiftMessage;
import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Service
public class Xlsx_MXRuler {
    public static void main(String[] args) {
        Map<String, Object> map = XmlToMap.parseXml("D:\\Win10Driver\\OneDrive\\桌面\\案例\\pacs.008.xml");
        sout(map,0);

    }
    private static void sout(Map<String, Object> map, int k){
        for (Map.Entry<String,Object> m:map.entrySet()){
            System.out.println();
            System.out.print(kg.substring(0,2*k));
            System.out.print(m.getKey() + ":");
            if(m.getValue() instanceof String){
                System.out.print(m.getValue());
            }else if(m.getValue() instanceof Map){
                sout((Map<String, Object>) m.getValue() , k+1);
            }else {
                List<Object> l = (List<Object>) m.getValue();
                for (Object o :l){
                    if(o instanceof String){
                        System.out.print(o + "\t");
                    }else {
                        sout((Map<String, Object>) o , k+1);
                    }
                }
            }
        }
    }
    private final static String kg = "                                                                                 " +
            "                                                                    ";

    /**
     * 报文校验
     * @param pathXlsx 决策表的物理位置
     * @param map 需要校验的报文
     * @return
     */
    public DroolsReturn XlsxMXRuler(String pathXlsx, Map<String,Object> map){
        String drl = getDrl(pathXlsx);

        KieHelper kieHelper = new KieHelper();
        kieHelper.addContent(drl, ResourceType.DRL);//读取drl的规则
        KieSession kieSession = kieHelper.build().newKieSession();

        DroolsReturn d = new DroolsReturn();//自定义的全局常量
        kieSession.setGlobal("retu",d);//放入全局变量
        kieSession.insert(map);//放入数据
        int i = kieSession.fireAllRules();//校验
        System.out.println("命中了" + i + "条规则");//输出命中几条规则
        kieSession.dispose();//关闭规则
        return d;
    }

    /**
     * 获取 xslx 的规则
     * @param pathXlsx
     * @return
     */
    private String getDrl(String pathXlsx) {
        File file = new File(pathXlsx);//读取文件
        InputStream is = null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        SpreadsheetCompiler compiler = new SpreadsheetCompiler();
        String drl = compiler.compile(is, InputType.XLS);//xlsx文件转成drl字符串

        System.out.println("------------------------------------");
        System.out.println(drl);//输出查看字符串样子
        System.out.println("------------------------------------");
        return drl;
    }
}
